// 接口的作用，在面向对象编程中，接口是一种规范的定义，他定义行为和动作的规范，在程序设计里面，接口起一种限制和规范的作用，
// 接口定义了某一批类所需遵守的规范，接口不关心这些类的内部数据，也不关心这些类里实现细节，它只规定这批类里必须提供某些方法，
// 提供这些方法的类可以满足需要，ts中的接口类似于Java，同时增加了更灵活的接口类型，包括属性，函数，可索引和类等

//1、属性接口 对json的约束
function f6(name: string, index: string) {
    console.log('f6');
}

f6('name', 'index');

/*
  ts中自定义方法传入参数时， json进行约束
*/
function f7(info: { name: string }) {
    console.log('f6');
}

f7({name: "xx"});

// f7({name: "xx", index: "xx"}); //error

/*属性约束*/
interface Info {
    name: string;
}

function f8(info: Info) {
    console.log('f8');
}

/*传入的参数必须满足name属性*/
let info: { name: 'index', age: 'xss' };

f8(info);//不报错

// f8({name:'index',age:'xss'}); //报错

//加密的函数类型接口
interface Enc {
    (key: string, value: string): string;
}

let enc: Enc = (key: string, value: string) => key + value;
console.log(enc('index', 'cc'));

let suits = ["hearts", "spades", "clubs", "diamonds"];

/*结构本质上属于模式匹配，只要等号两边的模式相同，等哈右边的变量就会被赋予等号左边的值*/
let [a1, b, c] = [2, 3, 4];

console.log(a1);

let [a2, [a3, [a4]]] = [21, [13, [23]]];
console.log(a2);
console.log(a3);
console.log(a4);

let [, , thread] = ['foo', 'bar', [1, 2], 12];
console.log(thread[0]);
console.log(thread[1]);

let array = [1, 2, 3, 4, 5, 6];

let arrays = [];
array.forEach((value, index) => {
    console.log(value);
    console.log(index);
    arrays.push(value);
});

console.log(arrays);

/*指定匿名对象地址指向引用*/
let content: { names: string, titles: string } = {names: 'Tom', titles: 'jack'};
console.log(content.names)

/*匿名对象*/
let {name, title} = {name: 'Tom', title: 'jack'};
console.log(name);

let people: {
    name?: string,
    address?: string,
    date?: Date,
} = {name: 'jack', address: 'Tony', date: new Date()};

console.log(people);

//结构的意义1.交换变量
let x = 1;
let y = 2;
[x, y] = [y, x];

//2.从函数获取多个参数
let example = () => {
    return [1, 2, 3];
}
let [x1, x2, x3] = example();
console.log(x1);
console.log(x2);

let example1 = () => {
    return {
        namesss: 'tom',
        titlesss: 'jack'
    };
}
let {namesss, titlesss} = example1();
console.log(example1().namesss)

//3.定义参数
let example3 = ([name, title]) => {

};

